package com.b1502.store2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;

import com.b1502.store2.R;
import com.b1502.store2.adapter.CategoryListShowAdapter;
import com.b1502.store2.model.bean.CategoryListShowBean;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * 作者：张宏达
 * 分类列表页面
 * <p>
 * TabLayout
 */
public class CategoryActivity extends BaseActivity {

    //声明变量
    CategoryListShowAdapter categoryListShowAdapter;
    XRecyclerView xrv_category_list_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        initData();
    }

    public void initData() {

        xrv_category_list_show = (XRecyclerView) findViewById(R.id.xrv_category_list_show);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        xrv_category_list_show.setLayoutManager(gridLayoutManager);
        categoryListShowAdapter = new CategoryListShowAdapter(this);

        //接受数据
        Intent intent = getIntent();
        String storeBarName = intent.getStringExtra("name");
        String userId = intent.getStringExtra("id");
        initStoreBar(storeBarName);
        getCategories(userId, 0);
    }

    //请求网络数据数据
    private void getCategories(String userId, int position) {

        StoreParams params = new StoreParams(UrlUtil.GetProducts, userId, position);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {

                //解析的数据封装到数组里面
                Gson gson = new Gson();
                CategoryListShowBean categoryListShowBean = gson.fromJson(result, CategoryListShowBean.class);
                List<CategoryListShowBean.ProductsBean> productsBeen = categoryListShowBean.getProducts();
                categoryListShowAdapter.addData(productsBeen);

                //设置适配器
                xrv_category_list_show.setAdapter(categoryListShowAdapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {


            }
        });
    }

}
